package com.dhp.security.config;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName: TokenLogoutHandler
 * @Description: 退出登录后，会删除token和redis里的数据
 * @Author: dhp
 * @Date: 2020/12/5 21:35
 * @Version: v1.0
 */
public class TokenLogoutHandler implements LogoutHandler {
    private RedisTemplate redisTemplate;

    public TokenLogoutHandler(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public void logout(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) {
        //从token中取得用户名，通过该用户名去删除redis里保存的权限数据
        String username = httpServletRequest.getHeader("token").replaceAll("fucktoken", "");
        redisTemplate.delete("username");
    }
}
